<?php

namespace app\common\libs;


use app\common\libs\pay\WxPayLib;
use app\common\traits\ResultTrait;
use Exception;
use yiqiniu\extend\traits\Singleton;
use yiqiniu\inside_sdk\Pay;

class OnlinePay
{

    use ResultTrait;
    use Singleton;

    private const DEFAULT_APP = 'billion_yard';
    /**
     * Pay 对象
     * @var Pay
     */
    protected $PayObj;
    /**
     * 支付帐号
     * @var string
     */
    protected $_accountNo = '';
    protected $_weixin_app_id = '';

    public function __construct()
    {
        $config = config("inside_config");
        if (empty($config)) {
            throw new Exception("缺少 inside_config 配置文件");
        }
        if (empty($config['pay']['account']) || empty($config['pay']['app_id'])) {
            throw new Exception(" inside_config 配置文件 中缺少支付帐号  和 app_id 的配置");
        }
        $this->_accountNo = $config['pay']['account'];
        $this->_weixin_app_id = $config['pay']['app_id'];

        $this->PayObj = new WxPayLib($config[self::DEFAULT_APP]["app_id"], $config[self::DEFAULT_APP]["app_key"], $config["url"]["pay"]);
    }


    /**
     * 调用支付接口
     * @param string $pay_order 支付单号
     * @param float $pay_money 支付金额
     * @param string $attach 附加参数 成功通知时 原样带回
     * @param string $memo 备注信息
     * @return array|void
     * @throws \Exception
     */
    public function PayMoney(string $pay_order, $pay_money, string $attach = '', string $memo = '')
    {
        try {
            $requestData = [
                'out_order_no' => $pay_order,
                'account_no' => $this->_accountNo,
                'scene' => 'native',
                'pay_money' => (float)$pay_money,
                'description' => $memo,
                'client_ip' => request()->ip(),
                'attach' => $attach,
                'profit_sharing' => 0,
                'app_id' => $this->_weixin_app_id,
            ];
            $response = $this->PayObj->Pay($requestData);
            return $this->result($response);
        } catch (Exception $e) {
            return $this->result($e);
        }
    }


    /**
     * 企业付款到零售
     * @param string $pay_order 付款单号
     * @param float $pay_money 支付金额
     * @param string $name 收款人姓名
     * @param string $open_id 收款人OPenID
     * @param string $memo 收款备注
     * @return array
     * @throws Exception
     */
    public function CompanyPay(string $pay_order, float $pay_money, string $name, string $open_id, string $memo = '扫码红包'): array
    {
        if (empty($name) || empty($open_id)) {
            throw  new Exception('接收人 openid 或 姓名 不能为空');
        }
        if ($pay_money <= 0) {
            throw  new Exception('付款多少必须大于0');
        }
        try {
            $requestData = [
                'out_order_no' => $pay_order,
                'account_no' => $this->_accountNo,
                'receiver_name' => $name,
                'pay_user_id' => $open_id,
                'reason' => $memo,
                'pay_money' => $pay_money,
                'client_ip' => request()->ip(),
                'app_id' => $this->_weixin_app_id,
            ];
            $response = $this->PayObj->CompanyPay($requestData);
            return $this->result($response);
        } catch (Exception $e) {
            return $this->result($e);
        }
    }

}